package com.github.xuemingyu.springboot.acticitycloud.controller;

import com.alibaba.fastjson.JSONObject;
import com.bidanet.bdcms.core.bean.ApiResult;
import com.dingtalk.api.response.OapiDepartmentListResponse;
import com.dingtalk.api.response.OapiUserGetResponse;
import com.github.xuemingyu.springboot.acticitycloud.common.JsonResult;
import com.github.xuemingyu.springboot.acticitycloud.config.DingConfig;
import com.github.xuemingyu.springboot.acticitycloud.dao.ActivityDao;
import com.github.xuemingyu.springboot.acticitycloud.dao.DepartmentDao;
import com.github.xuemingyu.springboot.acticitycloud.driver.dingtalk.DingTalkService;
import com.github.xuemingyu.springboot.acticitycloud.entity.Activity;
import com.github.xuemingyu.springboot.acticitycloud.entity.Department;
import com.github.xuemingyu.springboot.acticitycloud.entity.enumEntity.ActivityStatus;
import com.github.xuemingyu.springboot.acticitycloud.service.ActivityService;
import com.github.xuemingyu.springboot.acticitycloud.service.DepartmentService;
import com.github.xuemingyu.springboot.acticitycloud.service.SakuraEntryFormService;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: xmy
 * \* Date: 2019/1/30
 * \* Time: 14:23
 * \
 */
@RestController
@RequestMapping("/public")
public class PublicController {
    @Autowired
    private DingTalkService dingTalkService;
    @Autowired
    private DingConfig dingConfig;
    @Autowired
    private SakuraEntryFormService sakuraEntryFormService;
    @Autowired
    private DepartmentDao departmentDao;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ActivityDao activityDao;
    @Autowired
    private ActivityService activityService;

    @RequestMapping("/login")
    public ApiResult login(String code) throws ApiException {
        Map<String, String> s = dingTalkService.loginByCode(code);
        return ApiResult.success(s);
    }

    @RequestMapping("/pcLogin")
    public ApiResult pcLogin(String code) throws ApiException {
        Map<String, String> s = dingTalkService.pcLogin(code);
        if (Objects.nonNull(s) && s.size() > 0) {
            return ApiResult.success(s);
        }
         return ApiResult.error("抱歉，您无权访问!");
    }

    @RequestMapping("/getDepartmentList")
    public ApiResult getDepartmentList() throws ApiException {
        OapiDepartmentListResponse departmentList = dingTalkService.getDepartmentList();
        OapiDepartmentListResponse.Department department = new OapiDepartmentListResponse.Department();
        department.setId(1L);
        department.setName(dingConfig.getCompanyName());
        departmentList.getDepartment().add(department);
        return ApiResult.success(departmentList);
    }

    @RequestMapping("/findMemberForm")
    @ResponseBody
    public JsonResult getDept(String password, String startTime, String endTime) throws ApiException {
        JSONObject jsonArray = sakuraEntryFormService.synchronizationQuery(startTime, endTime, password);
        return JsonResult.success("", jsonArray);
    }

    @RequestMapping("/importUser")
    public ApiResult importUser(MultipartFile file) throws ApiException {
        departmentService.importUser(file);
        return ApiResult.success("导入成功");
    }

    @RequestMapping("/insertForm")
    public ApiResult insertForm(Long length) {
        sakuraEntryFormService.insertForm(length);
        return ApiResult.success("插入成功");
    }

    @RequestMapping("/test")
    public void test(Long id) throws ApiException {
       dingTalkService.getParentDepts("111");
        System.out.println();
    }
}
